package com.apposcopy.ella;

import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.Method;

import com.apposcopy.ella.dexlib2builder.MutableMethodImplementation;
import com.apposcopy.ella.dexlib2builder.instruction.BuilderInstruction35c;

public class ApplicationContextInstrumentor extends MethodInstrumentor {

	@Override
	protected void preinstrument(Method method, MutableMethodImplementation code) {

	}

	@Override
	protected void instrument(ClassDef classDef, Method method, MutableMethodImplementation code,
			int probeRegister) {
		//get the application context in the Activity.onCreate()
		//check the name and the parameter type of the method because the class name of the activity may be mixed
		if (method.getName().equals("onCreate")){
			if (method.getParameterTypes() != null 
					&& method.getParameterTypes().size() == 1
					&& method.getParameterTypes().get(0).equals("Landroid/os/Bundle;")){
				//invoke the method in ApplicationContextRecoder.
				//the bit num of the parameter must below 4.
				code.addInstruction(0, new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, probeRegister, 0, 0, 0, 0, probeMethRef));
			}
		}
	}

	@Override
	protected int numRegistersToAdd() {
		return 0;
	}

	@Override
	protected String probeMethName() {
		return "getContext";
	}

	@Override
	protected String recorderClassName() {
		return "com.apposcopy.ella.runtime.MyRecorder";
	}

}
